<?php

namespace app\common\model\xilujob;

use think\Model;
use app\common\model\User;

class ChatSession extends Model
{



    

    // 表名
    protected $name = 'xilujob_chat_session';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'updatetime_text'
    ];

    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return is_numeric($value) ? xilujob_time_tran($value) : $value;
    }

    // 可见状态:0=正常,1=发送人被拉黑(接收人不可见),2=接收人被拉黑(发送人不可见),3=相互拉黑(两方都不可见)
    public function getSeeTypeList()
    {
        return ['0' => __('正常'), '1' => __('发送人被拉黑(接收人不可见)'), '2' => __('接收人被拉黑(发送人不可见)'), '3' => __('相互拉黑(两方都不可见)')];
    }

    public function getSeeTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['see_type']) ? $data['see_type'] : '');
        $list = $this->getSeeTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function senduser()
    {
        return $this->belongsTo(User::class, 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function receiveuser()
    {
        return $this->belongsTo(User::class, 'receive_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function newmsg()
    {
        return $this->belongsTo('Chat', 'id', 'session_id')->field('id,session_id,type,message,see_type,createtime');
    }

    /**
     * 获取用户的
     * @param $myGroupId
     * @param $sendUserId
     * @param $receiveGroupId
     * @param $receiveUserId
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function checkSession($sendUserId,$receiveUserId){
        $xiluuser = \app\common\model\xilujob\XilujobUser::get(['user_id'=>$sendUserId]);
        $myGroupId = $xiluuser->group_id;
        //职位进程
        $receiveGroupId = $xiluuser->group_id == '2'?1:2;
        $session_data = self::field("*")
            ->where(function ($query) use ($sendUserId,$myGroupId,$receiveGroupId,$receiveUserId) {

                $query->where('send_group_id',$myGroupId)->where('send_user_id', $sendUserId)
                    ->where('receive_group_id',$receiveGroupId)->where('receive_user_id', $receiveUserId)
                    ->whereNull('senddeletetime')
                    ->whereOr(function ($query) use ($sendUserId,$myGroupId,$receiveGroupId,$receiveUserId) {
                        $query->where('receive_group_id',$myGroupId)->where('send_user_id', $receiveUserId)
                            ->whereNull('receivedeletetime')
                            ->where('send_group_id',$receiveGroupId)->where('receive_user_id', $sendUserId);
                    });
            })
            ->find();
        if(!$session_data){
            $companyId = $receiveGroupId == 2 ? CompanyProfile::where('user_id',$receiveUserId)->value('id') : CompanyProfile::where('user_id',$sendUserId)->value('id');
            //求职
            $resume =  $receiveGroupId == 1 ? Resume::getUserResume($receiveUserId) : Resume::getUserResume($sendUserId);
            $res = self::create([
                'send_group_id'=>   $myGroupId,
                'send_user_id' => $sendUserId,
                'receive_group_id'=> $receiveGroupId,
                'receive_user_id' => $receiveUserId,
                'resume_id'    =>   $resume ? $resume->id : 0,
                'company_id'    =>  $companyId,
                'see_type' => 0,
            ]);
            $session_data = self::get($res->id);
        }
        return $session_data;
    }

}
